<!-- 学生单选组件-->
<template>
  <el-dialog
    :visible.sync="visible"
    title="选择学生"
    width="800px"
    append-to-body
    @close="cancel"
  >
    <div class="app-container">
      <!-- 搜索表单 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="姓名" prop="xm">
              <el-input
                v-model="queryParams.xm"
                placeholder="请输入姓名"
                clearable
                prefix-icon="el-icon-user"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="学号" prop="xh">
              <el-input
                v-model="queryParams.xh"
                placeholder="请输入学号"
                clearable
                prefix-icon="el-icon-user"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="性别" prop="xb">
              <el-select v-model="queryParams.xb" placeholder="请选择性别" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="民族" prop="mz">
              <el-input
                v-model="queryParams.mz"
                placeholder="请输入民族"
                clearable
                prefix-icon="el-icon-office-building"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="xjxxList"
        @row-click="handleRowClick"
        border
        stripe
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column label="学生照片" align="center" prop="xsZp" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.xsZp" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="xm" width="120" />
<!--        <el-table-column label="学号" align="center" prop="xh" width="120" />-->
        <el-table-column label="性别" align="center" prop="xb" width="80">
          <template slot-scope="scope">
            <el-tag :type="scope.row.xb === '1' ? 'primary' : 'success'" size="mini">
              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.xb"/>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="出生日期" align="center" prop="scRq" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.scRq, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="民族" align="center" prop="mz" width="100" />
        <el-table-column label="政治面貌" align="center" prop="zzMm" width="120" />
        <el-table-column label="身份证号" align="center" prop="sfZh" width="180" />
        <el-table-column label="联系方式" align="center" prop="lxFs" width="120" />
      </el-table>

      <!-- 分页组件 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <span slot="footer" class="dialog-footer dialog-footer-center">
      <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
      <el-button size="small" @click="cancel">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {listJbxx} from "@/api/xsxx/jbxx"

export default {
  name: "XsXxDx", //学生单选组件
  dicts: ['xb'],
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      showSearch: true, // 显示搜索条件
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xm: null,
        xh: null,
        xb: null,
        mz: null
      },
      // 学生信息表格数据
      xjxxList: [],
      // 总条数
      total: 0,
      selectedRow: null
    };
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList();
      }
    }
  },
  methods: {
    getList() {
      this.loading = true;
      listJbxx(this.queryParams).then(response => {
        this.xjxxList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
        this.$message.error('获取学生列表失败');
      });
    },
    handleRowClick(row) {
      this.selectedRow = row; // 更新选中的行
    },
    submitForm() {
      if (!this.selectedRow) {
        this.$message.warning('请选择学生');
        return;
      }
      this.$emit('select', this.selectedRow);
      this.$emit('update:visible', false);
    },
    cancel() {
      this.$emit('update:visible', false);
      this.selectedRow = null;
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if (pageNum!== -1 && pageNum!== 0) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
}
</script>

<style scoped>
.dialog-footer-center {
  text-align: center;
}
.el-table {
  margin-top: 15px;
}
</style>
